function [neighbor_rf_nodes, nei, ns] = find_rf_nodes_gen(tri, cell, sides)
neighbor_rf_nodes = zeros(4,2,2);
nei = tri(cell).neighbor(nei_map(sides));
ns = find_sides(tri(nei),tri(cell).sides(:,:,sides));
i_gauss_nodes = tri(cell).edge_gauss(:,:,sides);
nf_gauss_nodes = i_gauss_nodes;
n_gauss_nodes = tri(nei).edge_gauss(:,:,ns);
if_gauss_nodes = n_gauss_nodes;
for i = 1 : size(i_gauss_nodes,1)
    tmp_nf_nodes = get_nodes(tri(nei).nodes, nf_gauss_nodes(i,:));
    tmp_if_nodes = get_nodes(tri(cell).nodes, if_gauss_nodes(i,:));
    neighbor_rf_nodes(i,:,1) = tmp_nf_nodes;
    neighbor_rf_nodes(i,:,2) = tmp_if_nodes;
end
end

function index = nei_map(side)
switch side
    case 1
        index = 3;
    case 2
        index = 2;
    case 3
        index = 1;
end
end

function index = find_sides(tri,node)
x_min = min([node(1,1),node(2,1)]);
x_max = max([node(1,1),node(2,1)]);
y_min = min([node(1,2),node(2,2)]);
y_max = max([node(1,2),node(2,2)]);
for i = 1 : 3
    tmp_x_min = min([tri.sides(1,1,i),tri.sides(2,1,i)]);
    tmp_x_max = max([tri.sides(1,1,i),tri.sides(2,1,i)]);
    tmp_y_min = min([tri.sides(1,2,i),tri.sides(2,2,i)]);
    tmp_y_max = max([tri.sides(1,2,i),tri.sides(2,2,i)]);
    if (x_min == tmp_x_min) && (x_max == tmp_x_max)
        if (y_min == tmp_y_min) && (y_max == tmp_y_max)
            index = i;
            return;
        end
    end
end
error("didn't find neighbor sides");
end

function edge_rf_nodes = get_nodes(nodes, edge_gauss)
%edge_rf_nodes = zeros(size(edge_gauss));
%edge_gauss: 物理单元坐标
%edge_rf_nodes: 参考单元坐标
Jacobi = [-nodes(1,1)+nodes(2,1), nodes(3,1) - nodes(1,1);
    -nodes(1,2)+nodes(2,2), nodes(3,2) - nodes(1,2)];
for i = 1 : 3
    for j = 1 : 4
        x = edge_gauss(1);
        y = edge_gauss(2);
        b = [x- nodes(1,1);y-nodes(1,2)];
        edge_rf_nodes = Jacobi\b;
        % disp(tmp);
        % disp([xi,eta]);
    end
end
end